﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using MyEntLibCMS.IDAL;

namespace MyEntLibCMS.DAL
{
    public class BaseRepository : IUnitOfWork
    {

        private DbConnection conn = null;
        public DbTransaction trans = null;

        public BaseRepository(DbConnection conn)
        {
            this.conn = conn;
        }

        /// <summary>
        /// 事务开启
        /// </summary>
        public void BeginTransaction()
        {
            if (this.conn.State == ConnectionState.Closed)
            {
                this.conn.Open();
            }
            trans = this.conn.BeginTransaction();
        }

        /// <summary>
        /// 事务提交
        /// </summary>
        public void Commit()
        {
            trans.Commit();
            if (this.conn.State == ConnectionState.Open)
            {
                this.conn.Close();
            }
        }

        /// <summary>
        /// 事务回滚
        /// </summary>
        public void Rollback()
        {
            trans.Rollback();
            if (this.conn.State == ConnectionState.Open)
            {
                this.conn.Close();
            }
        }
    }
}
